//
//  PictureImageView.m
//  meiyinxiang
//
//  Created by 媒印象 on 2018/1/5.
//  Copyright © 2018年 hbjApple. All rights reserved.
//

#import "PictureImageView.h"
@interface PictureImageView ()
@property (nonatomic,strong) NSMutableArray * imagViewArray;
@end
@implementation PictureImageView
- (void)awakeFromNib{
    [super awakeFromNib];
    [self createImgViwes];
}
-(void)setDetailModel:(MYXDetailViewModel *)detailModel{
    _detailModel = detailModel;
    if (detailModel.file.count > 0){
        if (detailModel.file.count == 1) {
            NSString *urlString = detailModel.file[0];
            UIImageView *subImgView = [self viewWithTag:10];
            subImgView.hidden = NO;
            [subImgView sd_setImageWithURL:[NSURL URLWithString:urlString] placeholderImage:[UIImage imageNamed:@"defual_luckyImg"]];
            CGFloat offSetX = (MYXPictureViewWith- detailModel.pictureViewSize.width)/2;
             CGRect rect = CGRectMake(offSetX, 5, detailModel.pictureViewSize.width, detailModel.pictureViewSize.height-MYXLuckyMoneyPictureViewOutterMargin);
            subImgView.frame = rect;
            [self.imagViewArray addObject:subImgView];
        }else{
            
            for (NSInteger i = 0; i < detailModel.file.count; i++) {
                NSString *urlString = detailModel.file[i];
                UIImageView *subImgView = [self viewWithTag:10 + i];
                subImgView.hidden = NO;
                [subImgView sd_setImageWithURL:[NSURL URLWithString:urlString] placeholderImage:[UIImage imageNamed:@"defual_luckyImg"]];
                [self.imagViewArray addObject:subImgView];
            }
        }
    }
}
//创建imgviews
- (void)createImgViwes{
    NSInteger count = 3;//九宫格3*3
    CGRect rect = CGRectMake(0, MYXLuckyMoneyPictureViewOutterMargin/2, MYXLuckyMoneyPictureItemWith, MYXLuckyMoneyPictureItemWith);
    for (NSInteger i = 0 ; i < count * count; i++) {
        UIImageView *imgview = [[UIImageView alloc] init];
        imgview.contentMode = UIViewContentModeScaleAspectFill;
        imgview.clipsToBounds = YES;
        //行
        NSInteger row = i / count;
        //列
        NSInteger clo = i % count;
        CGFloat xOffSet = clo * (MYXLuckyMoneyPictureItemWith + MYXLuckyMoneyPictureViewInnterMargin);
        CGFloat yOffSet = row * (MYXLuckyMoneyPictureItemWith + MYXLuckyMoneyPictureViewInnterMargin);
        imgview.frame = CGRectOffset(rect, xOffSet, yOffSet);
        imgview.hidden = YES;
        imgview.tag = 10 + i;
        [self addSubview:imgview];
        imgview.backgroundColor = [UIColor whiteColor];
        //添加手势
        UITapGestureRecognizer *tapGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAct:)];
        imgview.userInteractionEnabled = YES;
        [imgview addGestureRecognizer:tapGes];
    }
}
- (void)tapAct:(UIGestureRecognizer *)ges{
    NSInteger index = ges.view.tag - 10;
    HMPhotoBrowserController *photoBrowser =   [HMPhotoBrowserController photoBrowserWithSelectedIndex:index urls:self.detailModel.file parentImageViews:self.imagViewArray];
    [self.mSuperVC presentViewController:photoBrowser animated:YES completion:nil];

}
- (NSMutableArray *)imagViewArray{
    if (_imagViewArray == nil) {
        _imagViewArray = [NSMutableArray array];
    }
    return _imagViewArray;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
